<template>
  <div class="app-container">
<!--    <el-tabs v-model="activeName" @tab-click="handleClick">
      <el-tab-pane label="主销售员" name="first"></el-tab-pane>
      <el-tab-pane label="辅销售员" name="second"></el-tab-pane>
    </el-tabs>-->
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="客户名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入客户名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="手机号码" prop="selectPhone">
        <el-input
          v-model="queryParams.selectPhone"
          placeholder="请输入手机号码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="分级标签" prop="rankTag">
        <el-select v-model="queryParams.rankTag" placeholder="请选择" filterable clearable>
          <el-option
            v-for="dict in rankOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          ></el-option>
        </el-select>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['customer:sleep:edit']"
        >修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['customer:sleep:remove']"
        >删除
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="customerList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="id" align="center" prop="id"/>
      <el-table-column label="客户名称" align="center" prop="name"/>
      <el-table-column label="手机号码" align="center" prop="phone"/>
      <el-table-column label="分级标签" align="center" prop="rankTag">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.crm_customer_rank" :value="scope.row.rankTag"/>
        </template>
      </el-table-column>
      <el-table-column label="微信号" align="center" prop="wechat"/>
      <el-table-column label="邮箱" align="center" prop="email"/>
      <el-table-column label="数据状态" align="center" prop="state">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.crm_customer_state" :value="scope.row.state"/>
        </template>
      </el-table-column>
      <el-table-column label="客户类型" align="center" prop="type">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.crm_customer_type" :value="scope.row.type"/>
        </template>
      </el-table-column>
      <el-table-column label="客户规模" align="center" prop="firmSize">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.crm_customer_size" :value="scope.row.firmSize"/>
        </template>
      </el-table-column>
      <el-table-column label="销售人员" align="center" prop="mainSalerUser.userName"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-search"
            @click="handleRoam(scope.row)"
          >流转记录
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['customer:sleep:edit']"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['customer:sleep:remove']"
          >删除
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleLook(scope.row)"
          >查看详细
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="activeCustomer(scope.row)"
            v-hasPermi="['customer:sleep:active']"
          >激活
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="helpCustomer(scope.row)"
            v-hasPermi="['customer:sleep:help']"
          >求助
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleAssign(scope.row)"
            v-hasPermi="['customer:sleep:assign']"
          >指派
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <el-dialog :title="title" :visible.sync="openBusiness" width="500px" append-to-body>
      <el-form ref="formBusiness" :model="formBusiness" :rules="rulesBusiness" label-width="80px">
        <el-form-item label="选择业务" prop="businessId">
          <treeselect v-model="formBusiness.businessId" :options="businessList" :normalizer="normalizer"
                      placeholder="选择业务分类"/>
        </el-form-item>
        <el-form-item label="用户人数" prop="peopleNum">
          <el-input v-model="formBusiness.peopleNum" placeholder="请输入用户人数"/>
        </el-form-item>
        <el-form-item label="选择月份" prop="addTime">
          <el-date-picker
            v-model="formBusiness.addTime"
            type="month"
            placeholder="选择月"
            value-format="yyyy-MM"
          >
          </el-date-picker>
        </el-form-item>
      </el-form>

      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitBusinessForm">确 定</el-button>
        <el-button @click="cancelBussinness">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 添加或修改客户对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="客户名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入客户名称"/>
        </el-form-item>
        <el-form-item label="手机号码" prop="phone">
          <el-input v-model="form.phone" placeholder="请输入手机号码"/>
        </el-form-item>
        <el-form-item label="数据状态" prop="state">
          <el-select v-model="form.state" placeholder="请选择" filterable clearable>
            <el-option
              v-for="dict in stateOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="省" prop="provinceCode">
          <el-select v-model="form.provinceCode" placeholder="请选择省级地区" @change="getCity(form.provinceCode,'add')">
            <el-option
              v-for="dict in provinceOptions"
              :key="dict.id"
              :label="dict.name"
              :value="dict.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="市" prop="cityCode">
          <el-select v-model="form.cityCode" placeholder="请选择市级地区" @change="getArea(form.cityCode,'add')">
            <el-option
              v-for="dict in cityOptions"
              :key="dict.id"
              :label="dict.name"
              :value="dict.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="区" prop="areaCode">
          <el-select v-model="form.areaCode" placeholder="请选择区级地区">
            <el-option
              v-for="dict in areaOptions"
              :key="dict.id"
              :label="dict.name"
              :value="dict.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="地址" prop="address">
          <el-input v-model="form.address" placeholder="请输入地址"/>
        </el-form-item>
        <el-form-item label="企业简介" prop="detail">
          <el-input v-model="form.detail" type="textarea" placeholder="请输入内容"/>
        </el-form-item>
        <el-form-item label="微信号" prop="wechat">
          <el-input v-model="form.wechat" placeholder="请输入微信号"/>
        </el-form-item>
        <el-form-item label="邮箱" prop="email">
          <el-input v-model="form.email" placeholder="请输入邮箱"/>
        </el-form-item>
        <el-form-item label="客户规模" prop="firmSize">
          <el-select v-model="form.firmSize" placeholder="请选择" filterable clearable>
            <el-option
              v-for="dict in sizeOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>


    <!-- 用户导入对话框 -->
    <el-dialog :title="upload.title" :visible.sync="upload.open" width="400px" append-to-body>
      <el-upload
        ref="upload"
        :limit="1"
        accept=".xlsx, .xls"
        :headers="upload.headers"
        :action="upload.url + '?updateSupport=' + upload.updateSupport"
        :disabled="upload.isUploading"
        :on-progress="handleFileUploadProgress"
        :on-success="handleFileSuccess"
        :auto-upload="false"
        drag
      >
        <i class="el-icon-upload"></i>
        <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
        <div class="el-upload__tip text-center" slot="tip">
          <div class="el-upload__tip" slot="tip">
            <el-checkbox v-model="upload.updateSupport"/>
            是否更新已经存在的用户数据
          </div>
          <span>仅允许导入xls、xlsx格式文件。</span>
          <el-link type="primary" :underline="false" style="font-size:12px;vertical-align: baseline;"
                   @click="importTemplate">下载模板
          </el-link>
        </div>
      </el-upload>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitFileForm">确 定</el-button>
        <el-button @click="upload.open = false">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 查看详细 -->
    <el-dialog :title="titleLook" :visible.sync="openLook" width="500px" append-to-body>
      <el-form ref="form" :model="form" label-width="80px">
        <el-form-item label="客户名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入客户名称" disabled/>
        </el-form-item>
        <el-form-item label="手机号码" prop="phone">
          <el-input v-model="form.phone" placeholder="请输入手机号码" disabled/>
        </el-form-item>

        <el-form-item label="省级" prop="provinceCode">
          <el-select v-model="form.provinceCode" placeholder="请选择省级地区" @change="getCity(form.provinceCode,'add')"
                     disabled>
            <el-option
              v-for="dict in provinceOptions"
              :key="dict.id"
              :label="dict.name"
              :value="dict.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="市级" prop="cityCode">
          <el-select v-model="form.cityCode" placeholder="请选择市级地区" @change="getArea(form.cityCode,'add')" disabled>
            <el-option
              v-for="dict in cityOptions"
              :key="dict.id"
              :label="dict.name"
              :value="dict.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="区级" prop="areaCode">
          <el-select v-model="form.areaCode" placeholder="请选择区级地区" disabled>
            <el-option
              v-for="dict in areaOptions"
              :key="dict.id"
              :label="dict.name"
              :value="dict.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="地址" prop="address">
          <el-input v-model="form.address" placeholder="请输入地址" disabled/>
        </el-form-item>
        <el-form-item label="企业简介" prop="detail">
          <el-input v-model="form.detail" type="textarea" placeholder="请输入内容" disabled/>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" disabled/>
        </el-form-item>
      </el-form>
    </el-dialog>

    <!-- 指派   -->
    <el-dialog :title="titleAssign" :visible.sync="openAssign" width="500px" append-to-body>
      <el-form ref="formAssign" :model="formAssign" :rules="rulesAssign" label-width="80px">
        <el-form-item label="销售人员" prop="uid">
          <el-select v-model="formAssign.uid" placeholder="请选择" filterable clearable>
            <el-option
              v-for="user in userOptions"
              :key="user.userId"
              :label="user.userName"
              :value="user.userId"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="客户去向" prop="oper">
          <el-select v-model="formAssign.oper" placeholder="请选择" filterable clearable>
            <el-option label="回到私有" value="1"></el-option>
            <el-option label="保持现状" value="2"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitFormAssign">确 定</el-button>
        <el-button @click="cancelAssign">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 显示流转列表 -->
    <el-dialog title="流转记录" :visible.sync="roamOpen" width="1000px" append-to-body>
      <div>
        <el-table :data="roamList" style="margin-top: 10px">
          <el-table-column label="id" align="center" prop="id"/>
          <el-table-column label="客户名称" align="center" prop="crmCustomer.name"/>
          <el-table-column label="操作人" align="center" prop="createBy"/>
          <el-table-column label="操作时间" align="center" prop="createTime"/>
          <el-table-column label="内容" align="center" prop="content"/>
        </el-table>
      </div>
      <pagination
        v-show="roamTotal>0"
        :total="roamTotal"
        :page.sync="queryParamsRoam.pageNum"
        :limit.sync="queryParamsRoam.pageSize"
        @pagination="handleRoam"
      />
    </el-dialog>
  </div>
</template>

<script>
import {
  delCustomer,
  getCustomer,
  listCustomer,
  updateCustomer,
  updateSalerAssignSleep
} from "@/api/customer/signCustomer";
import {listBusiness} from "@/api/business/business"
import {addCustomerbusiness} from "@/api/customerbusiness/customerbusiness"
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
import {getToken} from "@/utils/auth";
import {getUser, getUserListByRoleKey, listUserNotPage} from "@/api/system/user";
import {listAreaAll} from "@/api/area/area";
import {listTranslog} from "@/api/translog/translog";

export default {
  name: "Customer",
  components: {Treeselect},
  dicts: ['crm_customer_state', 'crm_customer_size', 'crm_customer_rank', 'crm_customer_type', 'trace_type', 'task_type', 'task_status'],
  data() {
    return {
      // 查询参数
      queryParamsRoam: {
        orderByColumn: 'm.create_time',
        isAsc: 'desc',
        pageNum: 1,
        pageSize: 10,
        cid: null,
      },
      roamTotal: 0,
      roamOpen:false,
      roamList:[],
      stateOptions: [],
      userOptions: [],
      // 表单校验
      rulesAssign: {
        uid: [
          {required: true, message: "销售人员不能为空", trigger: "blur"},
        ],
        oper: [
          {required: true, message: "客户去向不能为空", trigger: "blur"},
        ],
      },
      // 弹出层标题
      titleAssign: "",
      // 是否显示弹出层
      openAssign: false,
      sizeOptions: [],
      rankOptions: [],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 客户表格数据
      customerList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,

      openBusiness: false,
      businessOptions: [],
      businessList: [],
      upload: {
        // 是否显示弹出层
        open: false,
        // 弹出层标题
        title: "",
        // 是否禁用上传
        isUploading: false,
        // 是否更新已经存在的用户数据
        updateSupport: 0,
        // 设置上传的请求头部
        headers: {Authorization: "Bearer " + getToken()},
        // 上传的地址
        url: process.env.VUE_APP_BASE_API + "/customerbusiness/customerbusiness/importData"
      },
      // 查询参数
      queryParams: {
        selectPhone: null,
        pageNum: 1,
        pageSize: 10,
        name: null,
        phone: null,
        rankTag: null,
        status: 7,
        state: null,
        provinceCode: null,
        cityCode: null,
        areaCode: null,
        address: null,
        detail: null,
        wechat: null,
        email: null,
        type: null,
        firmSize: null,
        oper:0
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        pid: [
          {required: true, message: "集团客户不能为空", trigger: "blur"}
        ],
        name: [
          {required: true, message: "客户名称不能为空", trigger: "blur"}
        ],
        phone: [
          { required: true, message: "手机号码不能为空", trigger: "blur" },
          {
            pattern: /^1[3|4|5|6|7|8|9][0-9]\d{8}$/,
            message: "请输入正确的手机号码",
            trigger: "blur"
          }
        ],
        provinceCode: [
          {required: true, message: "省级不能为空", trigger: "blur"}
        ],
        address: [
          {required: true, message: "详细地址不能为空", trigger: "blur"}
        ],
        email: [
          {required: false, message: "邮箱地址不能为空", trigger: "blur"},
          {type: 'email', message: '请输入正确的邮箱地址', trigger: 'blur'}
        ],
        type: [
          {required: true, message: "客户类型不能为空", trigger: "blur"},
        ],
        firmSize: [
          {required: true, message: "客户规模不能为空", trigger: "blur"},
        ],
      },
      rulesBusiness: {
        businessId: [
          {required: true, message: "业务类型不能为空", trigger: "blur"}
        ],
        peopleNum: [
          {required: true, message: "人数不能为空", trigger: "blur"}
        ],
        addTime: [
          {required: true, message: "月份不能为空", trigger: "blur"}
        ]
      },
      formBusiness: {},
      titleLook: '',
      openLook: false,
      formAssign: {},
      // 省级
      provinceOptions: [],
      // 市级
      cityOptions: [],
      // 区级
      areaOptions: [],
      cid:null,
      activeName: 'first',
    };
  },
  created() {
    this.getList();
    this.getProvince();
    this.getBusinessList();
    this.getSalesManList();
    this.getDicts("crm_customer_state").then(response => {
      this.stateOptions = response.data;
    });
    this.getDicts("crm_customer_size").then(response => {
      this.sizeOptions = response.data;
    });
    this.getDicts("crm_customer_rank").then(response => {
      this.rankOptions = response.data;
    })
  },
  methods: {
    handleClick(tab, event) {
      if(tab.index==0){
        this.activeName="first"
      }else {
        this.activeName="second"
      }
      this.queryParams.oper = tab.index
      this.getList()
    },
    //打开liuzhuan记录
    handleRoam(val) {
      if(val&&val.id){
        this.cid = val.id
        this.queryParamsRoam.cid = val.id
      }else {
        this.queryParamsRoam.cid=this.cid
      }
      listTranslog(this.queryParamsRoam).then(response => {
        this.roamList = response.rows;
        this.roamTotal = response.total;
        this.roamOpen = true;
      });
    },
    /** 查询销售列表 */
    getSalesManList(){
      var array = [];
      array.push("sales");
      getUserListByRoleKey(array).then(response => {
        if(response.code === 200){
          this.userOptions = response.data;
        }
      });
    },
    // 取消按钮
    cancelAssign() {
      this.openAssign = false;
      this.resetAssign();
    },
    /** 提交按钮 */
    submitFormAssign() {
      this.$refs["formAssign"].validate(valid => {
        if (valid) {
          let data = {
            cid: this.cid,
            uid: this.formAssign.uid,
            oper: this.formAssign.oper,
          }
          updateSalerAssignSleep(data).then(response => {
            this.$modal.msgSuccess("指派成功");
            this.openAssign = false;
            this.getList();
          });
        }
      });
    },
    // 表单重置
    resetAssign() {
      this.formAssign = {
        uid: null,
        oper:null,
        userName: null
      };
      this.cid = null
      this.resetForm("formAssign");
    },
    /** 指派按钮操作 */
    handleAssign(row) {
      this.resetAssign()
      this.openAssign = true;
      this.cid = row.id;
      this.titleAssign = "指派人员";
      if (row.mainSalerUser != null) {
        const id = row.mainSalerUser.userId
        getUser(id).then(response => {
          this.formAssign.userName = response.data.userName;
          this.formAssign.uid = row.mainSalerUser.userId;
        });
      }
    },
    /** 查看详细按钮操作 */
    handleLook(row) {
      this.reset();
      const id = row.id
      getCustomer(id).then(response => {
        this.form = response.data;
        this.openLook = true;
        this.titleLook = "详细信息";
        this.getCity(this.form.provinceCode, "edit");
        this.getArea(this.form.cityCode, "edit");
      });
    },
    activeCustomer(row) {
      this.$modal.confirm('是否确认激活客户业务管理编号为"' + row.id + '"的数据项？').then(function () {
        const data = {id: row.id, status: "9"}
        return updateCustomer(data);
      }).then(() => {
        this.getList();
        //this.loading = false;
        this.$modal.msgSuccess("客户已转为待激活客户");
      }).catch(() => {
      });
    },
    helpCustomer(row) {
      this.$modal.confirm('是否确认将客户业务管理编号为"' + row.id + '"的数据项转入全员营销？').then(function () {
        const data = {id: row.id, status: "4"}
        return updateCustomer(data);
        //this.loading = true;
      }).then(() => {
        this.getList();
        //this.loading = false;
        this.$modal.msgSuccess("客户已转到全员营销");
      }).catch(() => {
      });

    },
    formatterType(row) {
      if (row.type === '1') {
        return "企业客户"
      } else if (row.type === '2') {
        return "集团客户"
      }
    },
    formatState(row) {
      if (row.state === '1') {
        return "正常"
      } else if (row.state === '2') {
        return "失败"
      } else {
        return "删除"
      }
    },
    toRecordList(row) {
      this.$router.push({path: "/crm/sign/peopleListRecord", query: {id: row.id}})
    },
    /** 转换数据结构 */
    normalizer(node) {
      if (node.children && !node.children.length) {
        delete node.children;
      }
      return {
        id: node.id,
        label: node.name,
        children: node.children
      };
    },
    cancelBussinness() {
      this.openBusiness = false;
    },
    submitBusinessForm() {
      this.loading = true
      this.$refs["formBusiness"].validate(valid => {
        if (valid) {
          addCustomerbusiness(this.formBusiness).then(res => {
            this.$modal.msgSuccess("修改成功");
            this.openBusiness = false;
            this.getList();
          })
        }
      });
    },
    getBusinessList() {
      this.loading = true;
      listBusiness().then(res => {
        this.businessList = this.handleTree(res.data, "id", "pid");
        this.loading = false
      })
    },
    /** 查询客户列表 */
    getList() {
      this.loading = true;
      listCustomer(this.queryParams).then(response => {
        this.customerList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        name: null,
        phone: null,
        rankTag: null,
        status: "8",
        state: null,
        provinceCode: null,
        cityCode: null,
        areaCode: null,
        address: null,
        detail: null,
        wechat: null,
        email: null,
        type: null,
        firmSize: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null
      };
      this.resetForm("form");
    },
    resetBusiness() {
      this.formBusiness = {
        id: null,
        cid: null,
        businessId: null,
        peopleNum: null,
        addTime: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null
      }
      this.resetForm("formBusiness");
    },
    //获取区级地区
    getArea(val, type, oper) {
      this.areaOptions = []
      if (type !== "edit" && oper !== "query") {
        this.$set(this.form, 'areaCode', '');
      }
      if (oper === "query") {
        this.$set(this.queryParams, 'areaCode', '');
      }
      const data = {
        parentId: val,
        isEnabled: 1
      }
      listAreaAll(data).then(response => {
        this.areaOptions = response.data;
      });
    },
    //获取市级地区
    getCity(val, type, oper) {
      this.cityOptions = []
      this.areaOptions = []
      if (type !== "edit" && oper !== "query") {
        this.$set(this.form, 'cityCode', '');
        this.$set(this.form, 'areaCode', '');
      }
      if (oper === "query") {
        this.$set(this.queryParams, 'cityCode', '');
        this.$set(this.queryParams, 'areaCode', '');
      }
      let data = {
        parentId: val,
        isEnabled: 1
      }
      listAreaAll(data).then(response => {
        this.cityOptions = response.data;
      });
    },
    //获取省级
    getProvince() {
      const data = {
        parentId: 0,
        isEnabled: 1
      }
      listAreaAll(data).then(response => {
        this.provinceOptions = response.data;
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加客户";
    },
    handleAddPeople(row) {
      this.resetBusiness();
      this.openBusiness = true;
      this.title = "添加客户项目人数"
      this.formBusiness.cid = row.id
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getCustomer(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改客户";
        this.getCity(this.form.provinceCode, "edit");
        this.getArea(this.form.cityCode, "edit");
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateCustomer(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除客户编号为"' + ids + '"的数据项？').then(function () {
        return delCustomer(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('customer/customer/export', {
        ...this.queryParams
      }, `customer_${new Date().getTime()}.xlsx`)
    },
    /** 导入按钮操作 */
    handleImport() {
      this.upload.title = "数据导入";
      this.upload.open = true;
    },
    /** 下载模板操作 */
    importTemplate() {
      this.download('system/user/importTemplate', {}, `user_template_${new Date().getTime()}.xlsx`)
    },
    // 文件上传中处理
    handleFileUploadProgress(event, file, fileList) {
      this.upload.isUploading = true;
    },
    // 文件上传成功处理
    handleFileSuccess(response, file, fileList) {
      this.upload.open = false;
      this.upload.isUploading = false;
      this.$refs.upload.clearFiles();
      this.$alert("<div style='overflow: auto;overflow-x: hidden;max-height: 70vh;padding: 10px 20px 0;'>" + response.msg + "</div>", "导入结果", {dangerouslyUseHTMLString: true});
      this.getList();
    },
    // 提交上传文件
    submitFileForm() {
      this.$refs.upload.submit();
    }
  }
};
</script>
